<?php
/*
 * 商家控制器
 */
class sysmerchantCtrl extends lib_ctrl{
	
	public $merchant;
	
	public function _init(){
		$this->merchant = loadModel('merchant');
	}
	
	/*
	 * 添加商家
	 */
	public function addAct(){
		
		#AJAX提交注册
        if(!empty($_POST)){
			$userConfig = $this->loadConfig('user');#加载与用户相关的配置文件
			$userMod = $this->loadModel('user');
			$userID = $userMod->getNowUid();#获取当前管理员ID
			$item = $this->merchant->filterItem($_POST['item']);
			
            /* if('' == $item['name']) exit('-10');#
            if(!preg_match($userConfig['name_pat'], $item['name'])) exit('-11');#用户名格式不正确

            if('' == $item['email']) exit('-12');#
            if(!preg_match($userConfig['email_pat'], $item['email'])) exit('-13');#邮箱格式不正确

            if('' == $item['truename']) exit('-14');#
            if('' == $item['passwd']) exit('-15');#
            
            $plen = strlen($item['passwd']);
			if($plen < 6 || $plen > 20) exit('-16');#用户密码长度不对*/
            $item['createtime'] = $_SERVER['REQUEST_TIME'];
            $item['user_id'] = $userID;
            $password = $item['passwd'];
            
            if(isset($item['locale']) && $item['locale'] !=0){
            	$item['city_id'] = $item['locale'];
            }else if(isset($item['province']) && $item['province'] !=0 ){
            	$item['city_id'] = $item['province'];
            }else if(isset($item['city']) && $item['city'] !=0 ){
            	$item['city_id'] = $item['city'];
            }else{
            	$item['city_id'] = 0;
            }
            unset($item['city']);
            unset($item['province']);
            unset($item['locale']);
            $item['password'] = $userMod->createPasswd($item['passwd']);

            $res = $this->merchant->addMerchant($item);

            if($res){
            	exit('1');
			}
            exit($res);
        }
		
		$addressM= loadModel('address');
		$citys = $addressM->getCitys();
		$this->view->assign('citys',$citys[0]);
		$this->view->display('sysmkt/merchant_add.html');
		
	}
	
	/*
	 * 编辑商家
	 */
	
	public function editAct(){
		
		#AJAX提交注册
        if(!empty($_POST)){
            $userConfig = $this->loadConfig('user');#加载与用户相关的配置文件
			$item = $this->merchant->filterItem($_POST['item']);
			
            /*if('' == $item['id']) exit('-10');#
			if('' == $item['name']) exit('-11');#
            if(!preg_match($userConfig['name_pat'], $item['name'])) exit('-12');#用户名格式不正确

            if('' == $item['email']) exit('-13');#
            if(!preg_match($userConfig['email_pat'], $item['email'])) exit('-14');#邮箱格式不正确

            if('' == $item['truename']) exit('-15');#

			if('' == $item['passwd']) unset($item['passwd']);
			else{
				$plen = strlen($item['passwd']);
				if($plen < 6 || $plen > 20) exit('-16');#用户密码长度不对
                $password = $item['passwd'];
                $item['passwd'] = $this->user->createPasswd($item['passwd']);
			}*/
            $res = $this->merchant->editMerchant($item);

            #发送通知邮件
            if($res){
            	exit('1');
			}
            exit($res);
        }
		
		$id = abs(intval($_GET['id']));
        if(!$id) $this->forward('sysindex', 'error', array('msg'=>'缺少商家ID','link'=>1));
        $res = $this->merchant->setId($id)->setLimit(1)->select();
		$this->view->assign('res', $res);
        $this->view->display('sysmkt/merchant_edit.html');
	}
	
  /*
    #删除商家
    */
    function delAct()
    {
        if(!empty($_POST)){
	        $id = abs(intval($_POST['id']));
	        if(!$id)exit('-11');

            $res = $this->merchant->delMerchant($id);
            if($res) exit('1');
            exit($res);
        }
        exit('-10');
    }
	
	public function getCityChildrenAct(){
		$cityid = intval($_POST['cityid']);
		$addressM = loadModel('address');
		$citys = $addressM->getCityChildren($cityid);
		if($citys){
			echo json_encode($citys);
			exit('1');
		}else{
			exit('-1');
		}	
	}
}